import React from 'react';
import {Spin, Card, Button, Row, Col, Table, Form} from 'antd';
import PropTypes from 'prop-types';
import routes from './routes';
import {FORM_OPTIONS, TABLE, DATETIME_FORMAT} from '../constants';
import {approvePassData, approveBackData, approveRejectData, getDetail} from './api';
import {recommendedFullRate} from './constants';
import {conventEnumValueToString, formatDateTime} from '../utils';
import {exceptionInfoTransferApproveResult} from '../Enum';
import TextInput from 'Shared/components/TextInput';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import WrappedSelect from '../common/WrappedSelect';
import WrappedDatePicker from '../common/WrappedDatePicker';
import {injectIntl, language, formatMessage} from './intl';
import styles from './style.css';
import Return from '../common/Return';
import {newOrApproveOrderValidator} from './validator';
const FormItem = Form.Item;

const recommendedFullRateToList = recommendedFullRate.toList(language);
class ApprovePanel extends React.PureComponent {
    state = {
        loading: false,
        hasOperat: false,
        isValidate: false,
        data: {},
        approveDetails: [],
    }
    componentDidMount() {
        const id = this.props.id;
        this.setState({loading: true});
        getDetail(id)
            .then(res => {
                if(res.ok)
                    this.setState({
                        loading: false,
                        data: res.data,
                        approveDetails: res.data.approveDetails,
                    });
                else
                    this.setState({loading: false});
            });
    }
    componentWillUnmount() {
        this.isunmount = true;
    }
    onChange = (value, name) => {
        const data = Object.assign({}, this.state.data, {
            [name]: value
        });
        this.setState({
            data,
            hasOperat: true
        });
    }
    onClickOkOnPopconfirm = () => {
        this.props.onCancel();
    }
    onApprovePass = () => {
        const data = this.state.data;
        const isValid = newOrApproveOrderValidator(data);
        if(!isValid) {
            this.setState({
                isValidate: true
            });
            return;
        }
        const condition = {
            delayReason: data.delayReason,
            recommendedFullRate: data.recommendedFullRate,
            deadlineTime: data.deadlineTime,
            approveComment: data.approveComment,
            approveTime: data.approveTime,
            rowVersion: data.rowVersion,
        };
        this.setState({loading: true});
        approvePassData(this.props.id, condition).then(res => {
            if(res.ok)
                this.props.successReturn();
            if(!this.isunmount)
                this.setState({loading: false});
        });
    }
    onApproveBack = () => {
        const data = this.state.data;
        const isValid = newOrApproveOrderValidator(data);
        if(!isValid) {
            this.setState({
                isValidate: true
            });
            return;
        }
        const condition = {
            delayReason: data.delayReason,
            recommendedFullRate: data.recommendedFullRate,
            deadlineTime: data.deadlineTime,
            approveComment: data.approveComment,
            approveTime: data.approveTime,
            rowVersion: data.rowVersion,
        };
        this.setState({loading: true});
        approveBackData(this.props.id, condition).then(res => {
            if(res.ok)
                this.props.successReturn();
            if(!this.isunmount)
                this.setState({loading: false});
        });
    }
    onApproveReject= () => {
        const data = this.state.data;
        const isValid = newOrApproveOrderValidator(data);
        if(!isValid) {
            this.setState({
                isValidate: true
            });
            return;
        }
        const condition = {
            delayReason: data.delayReason,
            recommendedFullRate: data.recommendedFullRate,
            deadlineTime: data.deadlineTime,
            approveComment: data.approveComment,
            approveTime: data.approveTime,
            rowVersion: data.rowVersion,
        };
        this.setState({loading: true});
        approveRejectData(this.props.id, condition).then(res => {
            if(res.ok)
                this.props.successReturn();
            if(!this.isunmount)
                this.setState({loading: false});
        });
    }
    render() {
        const {approveDetails, data, isValidate, loading} = this.state;
        const approveColumns = [{
            title: formatMessage({
                id: 'approvePanel.column.approverName',
                defaultMessage: '审核人姓名'
            }),
            dataIndex: 'approverName',
        }, {
            title: formatMessage({
                id: 'approvePanel.column.approveTime',
                defaultMessage: '审核时间'
            }),
            dataIndex: 'approveTime',
            render: text => formatDateTime(text, DATETIME_FORMAT)
        }, {
            title: formatMessage({
                id: 'approvePanel.column.approveComment',
                defaultMessage: '审核意见'
            }),
            dataIndex: 'approveComment',
        }, {
            title: formatMessage({
                id: 'approvePanel.column.approvedResult',
                defaultMessage: '审核结果'
            }),
            dataIndex: 'approvedResult',
            render: text => conventEnumValueToString(exceptionInfoTransferApproveResult, text)
        }];
        return (
            <div className="form-standard">
                <Spin spinning={this.state.loading}>
                    <Card title={formatMessage({
                        id: 'approvePanel.column.common',
                        defaultMessage: '技术升级延期申请信息'
                    })}>
                        <Form>
                            <Row>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.delayReason',
                                        defaultMessage: '活动延期原因'
                                    })} {...FORM_OPTIONS.item} validateStatus={isValidate && !data.delayReason ? 'error' : null} required>
                                        <TextInput
                                            name="delayReason"
                                            value={data.delayReason}
                                            onBlur={this.onChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.recommendedFullRate',
                                        defaultMessage: '活动延迟建议完全比率'
                                    })} {...FORM_OPTIONS.item} validateStatus={isValidate && !data.recommendedFullRate ? 'error' : null} required>
                                        <WrappedSelect
                                            allowClear
                                            name="recommendedFullRate"
                                            value={data.recommendedFullRate}
                                            options={recommendedFullRateToList}
                                            onChange={this.onChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={formatMessage({
                                            id: 'addPanel.operation.deadlineTime',
                                            defaultMessage: '活动推迟截止时间'
                                        })}{...FORM_OPTIONS.item} validateStatus={isValidate && !data.deadlineTime ? 'error' : null} required>
                                        <WrappedDatePicker
                                            name="deadlineTime"
                                            value={data.deadlineTime}
                                            onChange={this.onChange}/>
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'approvePanel.operation.approveComment',
                                        defaultMessage: '审核意见'
                                    })} {...FORM_OPTIONS.item} validateStatus={isValidate && !data.approveComment ? 'error' : null} required>
                                        <TextInput
                                            name="approveComment"
                                            value={data.approveComment}
                                            onBlur={this.onChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'approvePanel.operation.approveTime',
                                        defaultMessage: '审核时间'
                                    })} {...FORM_OPTIONS.item} validateStatus={isValidate && !data.approveTime ? 'error' : null} required>
                                        {formatDateTime(data.approveTime, DATETIME_FORMAT)}
                                    </FormItem>
                                </Col>
                            </Row>
                        </Form>
                    </Card>
                    <Card>
                        <DescriptionList title={formatMessage({
                            id: 'approvePanel.table.approveDetails',
                            defaultMessage: '历史审核结果'
                        })}>
                            <Table
                                className="white-space-nowrap"
                                columns={approveColumns}
                                rowKey="id"
                                pagination={false}
                                dataSource={approveDetails}
                                {...TABLE} />
                        </DescriptionList>
                    </Card>
                    <Card className={styles.optionBtn}>
                        <Button
                            onClick={this.onApproveReject}
                            loading={loading}>
                            {formatMessage({
                                id: 'approvePanel.operation.onApproveReject',
                                defaultMessage: '审核拒绝'
                            })}
                        </Button>
                        <Button
                            onClick={this.onApproveBack}
                            loading={loading}>
                            {formatMessage({
                                id: 'approvePanel.operation.onApproveBack',
                                defaultMessage: '审核驳回'
                            })}
                        </Button>
                        <Button
                            type="primary"
                            onClick={this.onApprovePass}
                            loading={loading}>
                            {formatMessage({
                                id: 'approvePanel.operation.onApprovePass',
                                defaultMessage: '审核通过'
                            })}
                        </Button>
                    </Card>
                </Spin>
                <div className="page-toolbar">
                    <Return
                        disabled={this.state.loading}
                        onConfirm={this.onClickOkOnPopconfirm}
                        showWarning={this.state.hasOperat} />
                </div>
            </div>
        );
    }
}

ApprovePanel.propTypes = {
    successReturn: PropTypes.func.isRequired,
    onCancel: PropTypes.func.isRequired,
    history: PropTypes.object
};

import {connect} from 'react-redux';
import {
    tableSearch,
} from './actions';

const mapDispatchToProps = (dispatch, props) => ({
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
    onCancel: () => {
        props.history.push(routes.query.url());
    },
});
export default connect(null, mapDispatchToProps)(injectIntl(ApprovePanel));
